package com.nervenets.general.web;

import com.nervenets.general.Global;
import com.nervenets.general.entity.ResponsePageResult;
import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.i18n.I18nTranslator;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@SuppressWarnings("rawtypes")
public abstract class BaseController {
    protected <T> ResponseEntity<ResponseResult<T>> successMessage() {
        return this.successMessage(null);
    }

    protected final <T> ResponseEntity<ResponseResult<T>> successMessage(T object) {
        return this.successMessage(object, null, I18nTranslator.translate("code_200"));
    }

    protected final <T> ResponseEntity<ResponseResult<T>> successMessage(T object, String message) {
        return this.successMessage(object, null, message);
    }

    protected final <T> ResponseEntity<ResponseResult<T>> successMessage(T object, Map<String, ?> extra) {
        return this.successMessage(object, extra, I18nTranslator.translate("code_200"));
    }

    protected final <T> ResponseEntity<ResponseResult<T>> successMessage(T object, Map<String, ?> extra, String message) {
        ResponseResult<T> responseResult = ResponseResult.success();
        responseResult.setData(object);
        responseResult.setMessage(message);
        responseResult.setExtra(extra);
        return ResponseEntity.ok(responseResult);
    }

    protected final <T> ResponseEntity<ResponsePageResult<T>> successMessage(List<T> objects, long page, long total) {
        return this.successMessage(objects, page, total, null);
    }

    protected final <T> ResponseEntity<ResponsePageResult<T>> successMessage(List<T> objects, long page, long total, Map<String, ?> extra) {
        ResponsePageResult<T> responseResult = new ResponsePageResult<>();
        responseResult.setData(objects);
        responseResult.setMessage(I18nTranslator.translate("code_200"));
        responseResult.setPage(page);
        responseResult.setTotal(total);
        responseResult.setExtra(extra);
        return ResponseEntity.ok(responseResult);
    }

    protected ResponseEntity errorMessage(int messageCode) {
        return errorMessage(messageCode, I18nTranslator.translate("code_" + messageCode));
    }

    protected ResponseEntity errorMessage(String customMessage) {
        return errorMessage(customMessage, null);
    }

    protected <T> ResponseEntity errorMessage(int messageCode, T object) {
        return errorMessage(messageCode, object, I18nTranslator.translate("code_" + messageCode));
    }

    protected ResponseEntity errorMessage(int messageCode, String customMessage) {
        return errorMessage(messageCode, null, customMessage);
    }

    protected <T> ResponseEntity errorMessage(int messageCode, T object, String customMessage) {
        return ResponseEntity.status(messageCode).body(new ResponseResult<>(messageCode, object, customMessage));
    }

    protected <T> ResponseEntity errorMessage(String customMessage, T object) {
        return ResponseEntity.badRequest().body(new ResponseResult<>(HttpStatus.BAD_REQUEST.value(), object, customMessage));
    }

    protected void setJsonIgnoreEnv(String env, HttpServletRequest request) {
        request.setAttribute(Global.Constants.SESSION_ENV, env);
    }
}
